home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9561 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  63 lines

  1. Path: news.compulink.co.uk!usenet
  2. From: John@zeitgeist.compulink.co.uk (John)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Templates problem: I don't get this :(
  5. Date: Sat, 02 Mar 1996 19:06:43 GMT
  6. Organization: PPL
  7. Message-ID: <4ha639$fr2@zinc.compulink.co.uk>
  8. References: <4h6unr$3hu@wsintt09.win.tue.nl>
  9. Reply-To: John@zeitgeist.compulink.co.uk
  10. NNTP-Posting-Host: zeitgeist.compulink.co.uk
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. dedos8@wsintt09.win.tue.nl (Arnaud Gouder de Beauregard) wrote:
  14.  
  15. >I tried to make a Binary Tree with templates, but the following won't
  16. >compile: What am I doing wrong ?
  17. Try instead:
  18.  
  19. template <class T>
  20. class btree
  21. {
  22. public:
  23.     // to set nodeval, need valid  = operator !!!
  24.      btree( T& val ) : nodeval( val ), leftNode( 0 ), rightNode( 0 )
  25.      {}
  26.  
  27.      void addNode( T& arg1 )
  28.      {
  29.           if ( arg1 <= nodeval )
  30.                 if ( ! leftNode )
  31.                      leftNode = new btree( arg1 );
  32.                 else
  33.                      leftNode->addNode( arg1 );
  34.           else
  35.                 if ( ! rightNode )
  36.                      rightNode = new btree( arg1 );
  37.                 else
  38.                      rightNode->addNode( arg1 );
  39.      }
  40.  
  41. private:
  42.      T       nodeval;
  43.      btree*  leftNode;
  44.      btree*  rightNode;
  45. };
  46.  
  47. btree <double> aTree( 10.0 );
  48.  
  49. void f()
  50. {
  51.      aTree.addNode( 9.0 );
  52. }
  53.  
  54. Note that there is plenty missing from the above and is not intended as an
  55. example of good coding !
  56. Better still, re-use an existing btree class, but I guess you're doing this as
  57. a learning exercise :-)
  58. Regards,
  59. John    (Freelance technical consultant).
  60. Regards,
  61. John.
  62.  
  63.